package com.wiseman.writing;

import java.util.ArrayList;

import com.wiseman.writing.utility.database.WDatabase;


import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class HistoryActivity extends ListActivity implements ConstValue{
	
	private ArrayList<StrokeDBData> mDataList = new ArrayList<StrokeDBData>();
	private WDatabase mDatabase = null;
	private WriteRecoadAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		
		mDatabase  = new WDatabase(this, TABLE_NAME) {
			@Override
			public void fillKeys() {
				mKeyMap.put(KEY_CHARS, "TEXT");
			}
		};

		mDatabase.open();
		
		loadData();
		
		mAdapter = new WriteRecoadAdapter(this, mDataList);
		setListAdapter(mAdapter);	
		
		DomoAd.createBar(this);		
	}	
	
	@Override
	protected void onDestroy() {
		mDatabase.close();
		super.onDestroy();
	}
	
	private void loadData() {
	
		Cursor c = mDatabase.fetchAllData();
	
		while (c.moveToNext()) {
			StrokeDBData data = new StrokeDBData();
			data.getData(c);		
			mDataList.add(data);			
		}
		
		c.close();
	}

	class WriteRecoadAdapter extends BaseAdapter {

		ArrayList<StrokeDBData> mData = null;
		Context mContext = null;

		public WriteRecoadAdapter(Context context, ArrayList<StrokeDBData> data) {
			mContext = context;
			mData = data;
		}

		public void dataChange(ArrayList<StrokeDBData> data) {
			mData = data;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			return mData.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = null;

			if (convertView == null) {
				LayoutInflater mInflater = LayoutInflater.from(mContext);
				view = mInflater.inflate(R.layout.listitem, parent, false);
			} else {
				view = convertView;
			}
			
			setItem(view, position);

			return view;
		}

		void setItem(View v, int pos) {	
			
			int bgColor = getResources().getColor(pos%2 == 0?R.color.list_background1:R.color.list_background2);
		
			v.setBackgroundColor(bgColor);
			StrokeDBData data = mData.get(pos);
			if (data != null) {
				TextView tvData = (TextView) v.findViewById(R.id.list_date);
				tvData.setText(data.toDateString());
				
				GridView gv = (GridView) v.findViewById(R.id.list_chars);
				CharsAdapter adapter = new CharsAdapter(mContext, data.mChars, true);
				gv.setAdapter(adapter);				
				gv.setOnItemClickListener(adapter);
			}

		}
	}

}
